Gradle 配置常见问题

常见gradle配置记录,如输出应用包名,相关debug和release版本信息分开配置等,对于gradle中常见问题如版本重复依赖版本冲突等问题的处理记录。

输出包名配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static def releaseTime() {
return new Date().format("yyyyMMdd_hhmm")
}
android{
...
//修改apk包名
android.applicationVariants.all { variant ->
variant.outputs.all {

if (outputFileName != null && outputFileName.endsWith(".apk")) {
def endStr = outputFileName
if (outputFileName.contains("debug")) {
endStr = "debug.apk"
}

if (outputFileName.contains("release")) {
endStr = "release.apk"
}
outputFileName = "app" + "_${releaseTime()}_${defaultConfig.versionName}_${defaultConfig.versionCode}_" + endStr
}

}
}
...
}

对于项目中出现重复依赖且版本冲突处理方式

参考资料链接

对于出现项目依赖 Rxjava 3.x 版本,而同时使用 RxAndroid 2.x 版本(RxAndroid 2.x 版本内部依赖 Rxjava 2.x 版本)出现的 Rxjava 版本冲突最终处理如下:统一项目使用的 Rxjava 版本。

  1. 全局替换重复依赖

    1
    2
    3
    4
    5
    6
    configurations.all {
    resolutionStrategy.dependencySubstitution {
    // Substitute one module dependency for another
    substitute module("io.reactivex.rxjava2:rxjava:2.2.0") with module("io.reactivex.rxjava3:rxjava:3.0.0-RC0")
    }
    }
  2. 全局排除重复依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    configurations {
    compile.exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
    all {
    resolutionStrategy {
    eachDependency { details ->
    if (details.requested.group == 'io.reactivex.rxjava3' &&
    details.requested.name == 'rxjava') {
    details.useVersion '3.0.0-RC0'
    details.because 'Unified the version of RxJava3.'
    }
    }
    }
    }
    }

    dependencies {
    // ...
    implementation rootProject.ext.dependencies['rxjava3']
    implementation rootProject.ext.dependencies['rxandroid']
    }
  3. 指定重复依赖项单独配置排除(不推荐):

    1
    2
    3
    implementation ('io.reactivex.rxjava2:rxandroid:2.1.0')  {
    exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
    }

替换本地与远程依赖(可以在开发 Library 时使用,方便切换本地库和在线库)

1
2
3
4
5
6
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module("org.utils:api") because "we work with the unreleased development version" with project(":api")
substitute module("org.utils:util:2.5") with project(":util")
}
}

通用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext {
// SDK and tools
compileSdkVersion = 28
minSdkVersion = 21
targetSdkVersion = 28

// App dependencies
appCompatVersion = '1.0.2'
cardVersion = '1.0.0'
materialVersion = '1.0.0'
recyclerViewVersion = '1.0.0'
androidXAnnotations = '1.0.1'
timberVersion = '4.7.1'
}
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()

}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
apply plugin: 'com.android.application'

// app_icon
def debugIcon = "@mipmap/ic_launcher"
def betaIcon = "@mipmap/ic_launcher"
def prodIcon = "@mipmap/ic_launcher"

// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
def keystorePropertiesFile = rootProject.file("keystore.properties")
// 根目录文件
def jksFile = rootProject.file("markzl.jks")

def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
applicationId "com.markzl.android.gradlesetting"
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

signingConfigs {
release {
storeFile jksFile
storePassword keystoreProperties.getProperty("storePassword")
keyAlias keystoreProperties.getProperty("keyAlias")
keyPassword keystoreProperties.getProperty("keyPassword")

}
}
buildTypes {
release {
signingConfig signingConfigs.release
buildConfigField("String", "URL_HOST", "\"正式环境地址\"")
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
buildConfigField("String", "URL_HOST", "\"测试环境地址\"")
}
}
flavorDimensions 'default'
productFlavors {
dev {
dimension = 'default'
applicationIdSuffix = '.dev'
resValue("string", "app_title", "应用名_测试版")
resValue("string", "baidu_api_key", "测试版key")
manifestPlaceholders = [app_icon: debugIcon]
}
beta {
dimension = 'default'
applicationIdSuffix = '.beta'
resValue("string", "app_title", "应用名_线上测试版")
resValue("string", "baidu_api_key", "线上测试版key")
manifestPlaceholders = [app_icon: betaIcon]
}
prod {
dimension = 'default'
resValue("string", "app_title", "应用名_线上测试版")
resValue("string", "baidu_api_key", "正式版key")
manifestPlaceholders = [app_icon: prodIcon]
}
}

android.variantFilter { variant ->
def flavors = variant.getFlavors()
if (!flavors.isEmpty()) {
def flavorName = flavors.get(0).name
def buildTypeName = variant.buildType.name
def ignoreVariant = true

switch (buildTypeName) {
case "release":
switch (flavorName) {
case "prod":
case "beta":
ignoreVariant = false
break
}
break
case "debug":
switch (flavorName) {
case "dev":
ignoreVariant = false
break
}
break
}
variant.setIgnore(ignoreVariant)
}
}
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def flavorName = variant.getFlavorName()
def buildType = variant.buildType.getName()
def versionCode = defaultConfig.versionCode
outputFileName = "应用名-${flavorName}-${buildType}.${versionCode}.apk"
}
}
}


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// App dependencies
implementation "androidx.appcompat:appcompat:$appCompatVersion"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.cardview:cardview:$cardVersion"
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.annotation:annotation:$androidXAnnotations"
implementation "com.jakewharton.timber:timber:$timberVersion"

// Architecture Components

// Dependencies for local unit tests
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'



}

/**
* @return The most recent git tag to be used as version name for the app.
*/
def getGitVersion() {
def cmd = "git describe --tag"
try {
def proc = cmd.execute()
return proc.text.trim()
} catch (IOException e) {
//Unable to find 'git'
return "please update version name manually"
}
}

def releaseTime() {
return new Date().format("yyyyMMdd_hhmm")
}
# gradle
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×